﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class NghienCuu
    {
        //ID
        public String maNghienCuu;
        public String maDiaDiem;

        //Properties
        DateTime ngayBatDau;
        DateTime ngayKetThuc;
        String noiDungNghienCuu;
        String tenLuanAn;
        DateTime ngayBaoVe;
        String noiBaoVe;
        String nguoiHuongDan;
        float ketQua;

        // object
        DiaDiemNghienCuu diaDiemNghienCuu
        {
            get
            {
                return new DiaDiemNghienCuu(maDiaDiem);
            }
            set
            {
                diaDiemNghienCuu = value;
            }
        }

        //database
        Database myDatabase = Database.shareDatabase;

        public NghienCuu(DateTime _ngayBatDau, DateTime _ngayKetThuc, String _noiDungNghienCuu, String _tenLuanAn, DateTime _ngayBaoVe, String _noiBaoVe, String _nguoiHuongDan, float _ketQua,DiaDiemNghienCuu _diemDiemNghienCuu)
        {
            //ID
            maNghienCuu = Database.getID("NghienCuu", "MaNghienCuu");
            maDiaDiem = _diemDiemNghienCuu.maDiaDiem;

            //Properties
            ngayBatDau = _ngayBatDau;
            ngayKetThuc = _ngayKetThuc;
            noiDungNghienCuu = _noiDungNghienCuu;
            tenLuanAn = _tenLuanAn;
            ngayBaoVe = _ngayBaoVe;
            noiBaoVe = _noiBaoVe;
            nguoiHuongDan = _nguoiHuongDan;
            ketQua = _ketQua;

            // object
            diaDiemNghienCuu = _diemDiemNghienCuu;

            
        }
        public NghienCuu(String _maNghienCuu)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM NghienCuu WHERE MaNghienCuu = '" + _maNghienCuu + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                //ID
                maNghienCuu = table.Rows[0]["MaNghienCuu"].ToString();
                maDiaDiem = table.Rows[0]["MaDiaDiem"].ToString();

                //Properties
                ngayBatDau = table.Rows[0].Field<DateTime>("NgayBatDau");
                ngayKetThuc = table.Rows[0].Field<DateTime>("NgayKetThuc");
                noiDungNghienCuu = table.Rows[0]["NoiDungNghienCuu"].ToString(); 
                tenLuanAn = table.Rows[0]["TenLuanAn"].ToString();
                ngayBaoVe = table.Rows[0].Field<DateTime>("NgayBaoVe");
                noiBaoVe = table.Rows[0]["NoiBaoVe"].ToString(); 
                nguoiHuongDan = table.Rows[0]["NguoiHuongDan"].ToString(); 
                ketQua = float.Parse(table.Rows[0]["KetQua"].ToString()); 

            }
        }
        public Boolean Save()
        {
            String sqlNghienCuuSinh = "INSERT INTO NghienCuu VALUES('" 
                + maNghienCuu + "','" 
                + Database.sqlDateTime(ngayBatDau) + "','" 
                + Database.sqlDateTime(ngayKetThuc) + "','"
                + maDiaDiem + "',N'"
                + noiDungNghienCuu + "',N'" 
                + tenLuanAn + "','"
                + Database.sqlDateTime(ngayBaoVe) + "',N'"
                + noiBaoVe + "',N'" 
                + nguoiHuongDan + "'," 
                + ketQua + ")";
            QueryResult result = myDatabase.Query(sqlNghienCuuSinh);

            if (result.ReturnCode == 409)
            {
                return false;
            }
            else
            {
                diaDiemNghienCuu.Save();
                return true;
            }
        }
        public Boolean Update()
        {
            bool isDupplicated = Database.isDupplicate("NghienCuu", "MaNghienCuu", maNghienCuu);

            // update
            if (isDupplicated)
            {
                String sqlUpdate = "UPDATE NghienCuu SET " 
                    + "NgayBatDau = '" + Database.sqlDateTime(ngayBatDau)
                    + "', NgayKetThuc = '" + Database.sqlDateTime(ngayKetThuc)
                    + "', NoiDungNghienCuu = N'" + noiDungNghienCuu
                    + "', TenLuanAn = N'" + tenLuanAn 
                    + "', NgayBaoVe = '" + Database.sqlDateTime(ngayBaoVe)
                    + "',NoiBaoVe = N'" + noiBaoVe
                    + "',NguoiHuongDan = N'" + nguoiHuongDan 
                    + "',KetQua = " + ketQua
                    + " WHERE MaNghienCuu = '" + maNghienCuu + "'";
                QueryResult result = myDatabase.Query(sqlUpdate);

                if (result.ReturnCode == 409)
                    return false;
                else
                {
                    return true;
                }

            }
            return false;
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM NghienCuu WHERE MaNghienCuu = '" + maNghienCuu + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                diaDiemNghienCuu.Delete();
                return true;
            }
        }
    }
}
